<template>
	<view>
		<web-view :src="webviewurl" @message="handleMessage"></web-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				total: '',
				paypal_id: '',
				out_trade_no: '',
				return_url: '',
				webviewurl: ''
				//webviewurl: '/static/hybrid/html/local.html'
			};
		},
		onLoad(opt) {
			console.log(opt)
			if (opt.total) {
				this.total = decodeURIComponent(opt.total);
			}
			if (opt.out_trade_no) {
				this.out_trade_no = decodeURIComponent(opt.out_trade_no);
			}
			if (opt.paypal_id) {
				this.paypal_id = opt.paypal_id;
				// console.log(this.webviewurl)
			}
			// 构造带参数的 URL
			//this.webviewurl = this.return_url;
			this.webviewurl =
				`/hybrid/html/local.html?total=${this.total}&out_trade_no=${this.out_trade_no}&paypal_id=${this.paypal_id}`;



			// #ifdef H5
			// uniapp webview h5 通信 window.postMessage 方式
			// https://blog.csdn.net/qq_42313445/article/details/127068570
			window.addEventListener("message", (event) => {
				// 全量打印
				// console.log(event.data)
				if(event.data.oid){
					console.log(event.data.oid)
					let param = {
						paypal_id: event.data.oid
					}
					let url = this.site_url + 'api/auth/home/paypalCapture';
					this.areq(url, param, 'POST').then(res => {
						if (res.code == 1) {
					
						}
					
					})
					
					uni.redirectTo({
						url: '/pages/index/cuccessful'
					})
				}

			}, false)
			// #endif


		},
		methods: {
			handleMessage(data) {
				console.log('event');
				console.log(data);
				let param = {
					paypal_id: data.oid
				}
				let url = this.site_url + 'api/auth/home/paypalCapture';
				this.areq(url, data, 'POST').then(res => {
					if (res.code == 1) {

					}

				})


			},
		}

	}
</script>
<style>
</style>